package com.miku.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	public final static String fromBytes(byte[] s) {
		if(s == null) return null;
	    MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
		    byte[] digest = md5.digest(s);
		    String hashString = new BigInteger(1, digest).toString(16);
		    return hashString.toUpperCase();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
    }
	
	public final static String fromString(String s) {
		byte[] uploadBytes = s.getBytes();
	    MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
		    byte[] digest = md5.digest(uploadBytes);
		    String hashString = new BigInteger(1, digest).toString(16);
		    return hashString.toUpperCase();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
    }
}
